import { defineStore, createPinia } from 'pinia'
import router from '@/router/index'
import { routerMap } from '../router/index'
import { getUserPurview } from '@/api/loginApi'

export const useMainStore = defineStore('main', {
  state: () => {
    return {
      menu: null,
      paths: [],
    }
  },
  actions: {
    async getMenu (path, str) {
      str = '获取权限菜单'
      const loading2 = ElLoading.service({
        lock: true, text: `${str}...`,
        background: "rgba(0, 0, 0, 0.7)",
      });
      const data = await getUserPurview()
      if (loading2) { setTimeout(() => { loading2.close() }, 2000) }
      this.menu = data
      if (data.length > 0) {
        //如果权限菜单不为空正常进行权限授权
        this.initRouter(data, path)
      } else {
        //如果权限列表为空提示
        ElMessage.error('很抱歉！您暂时无操作权限，请联系系统管理员开通！')
      }
    },
    initRouter (menu, path) {
      routerMap.forEach(i => router.addRoute('Layout', i))
      router.push(path ? path : '/')
    }
  }
})

export default createPinia();
